
// vis list
MuseX.VisualizationList = {};

/*------------------------------------
	reload visualizations command
------------------------------------*/
concommand.Add( "musex_reloadvisualizations", function( pl, cmd, args )

	// load visualizations
	MuseX.LoadVisualizations();
	
end );


/*------------------------------------
	load visualizations from directory
------------------------------------*/
function MuseX.LoadVisualizations( )

	// skin path
	local path = "musex/visualizations/";

	// find .lua files in path
	local visualizations = file.FindInLua( ("%s*.lua"):format( path ) );
	
	// loop through list of files
	for _, vis in pairs( visualizations ) do
	
		// create skin object
		_G[ 'VIS' ] = {};
	
		// include file
		include( path .. vis );
		
		// nil object
		_G[ 'VIS' ] = nil;
		
	end

end


/*------------------------------------
	define visualization by name
------------------------------------*/
function MuseX.DefineVisualization( name, tbl )

	// store name
	tbl.Name = name;
	
	// create the GetSetting function
	tbl.GetSetting = function( self, name )
	
		// type variable
		local stype;
	
		// loop through list of settings
		for _, setting in pairs( self.Settings ) do
		
			// match setting name
			if ( setting[ 1 ] == name ) then
			
				// save type
				stype = setting[ 3 ];
				
				// break loop
				break;
			
			end
		
		end
	
		// build the new setting name
		local settingname = ("VIS_%s_%s"):format( self.Name, name );
		
		// return setting value
		return MuseX.GetSetting( settingname, stype );
	
	end
	
	// create the SetSetting function
	tbl.SetSetting = function( self, name, value )
		
		// build the new setting name
		local settingname = ("VIS_%s_%s"):format( self.Name, name );
		
		// save the value
		MuseX.SetSetting( settingname, value );
	
	end
	
	// add to list
	MuseX.VisualizationList[ name ] = tbl;
	
	// setup any settings
	if ( tbl.Settings && type( tbl.Settings ) == "table" ) then
	
		// loop through list of settings
		for k, setting in pairs( tbl.Settings ) do
		
			// make sure the setting is a table and the right length
			if ( type( setting ) == "table" && #setting >= 3 ) then
			
				// make sure it is formatted properly
				if ( type( setting[ 1 ] ) == "string" && setting[ 2 ] != nil && type( setting[ 3 ] ) == "number" ) then
				
					// check if it already exists
					if ( !tbl:GetSetting( setting[ 1 ] ) ) then
					
						// create the setting
						tbl:SetSetting( setting[ 1 ], setting[ 2 ] )
					
					end
					
				else
				
					// remove the setting if improperly formatted
					tbl.Settings[ k ] = nil;
				
				end
				
			else
			
				// remove the setting if improperly formatted
				tbl.Settings[ k ] = nil;
			
			end
		
		end
		
	else
	
		// remove the entire settings table if improperly formatted
		tbl.Settings = nil;
	
	end

end


/*------------------------------------
	initialize visualization
------------------------------------*/
function MuseX.InitVisualization( )

	// get the visualization name
	local vis = MuseX.GetSetting( "visualization" );
	
	// make sure it isn't diabled
	if ( vis == "none" ) then
	
		// nothing to do
		return;
		
	end

	// get visualization table
	local tbl = MuseX.VisualizationList[ vis ];
	
	// make sure it exists
	if ( tbl && tbl.Init ) then
	
		// call init
		tbl:Init();
	
	end
	
end


/*------------------------------------
	draw visualization
------------------------------------*/
function MuseX.RenderVisualization( panel )

	// make sure a song is playing
	if ( !MuseX.IsPlaying() ) then
	
		// TODO: Change this?
		// dont draw
		return;
		
	end
	
	// get the visualization name
	local vis = MuseX.GetSetting( "visualization" );
	
	// make sure it isn't diabled
	if ( vis == "none" ) then
	
		// nothing to do
		return;
		
	end
	
	// get visualization table
	local tbl = MuseX.VisualizationList[ vis ];

	// make sure it exists
	if ( tbl && tbl.Render ) then
	
		// call render function
		tbl:Render( panel, MuseX.Player:GetCurrentPosition(), MuseX.Player:GetDuration() );
	
	end

end

/*------------------------------------
	visualization think
------------------------------------*/
function MuseX.VisualizationThink( )

	// make sure a song is playing
	if ( !MuseX.IsPlaying() ) then
	
		// TODO: Change this?
		// dont think
		return;
		
	end
	
	// get the visualization name
	local vis = MuseX.GetSetting( "visualization" );
	
	// make sure it isn't diabled
	if ( vis == "none" ) then
	
		// nothing to do
		return;
		
	end
	
	// get visualization table
	local tbl = MuseX.VisualizationList[ vis ];
	
	// make sure it exists
	if ( tbl && tbl.Think ) then
	
		// call render function
		tbl:Think( MuseX.Player:GetCurrentPosition(), MuseX.Player:GetDuration() );
	
	end

end
